<template>
<div class="roles">

  <el-card>
<el-row class="rowBtn">
  <el-col><el-button type="primary" @click="addDialogVisible=true">添加用户</el-button></el-col>
</el-row>
<el-table :data="rolesList"
          border
          stripe>
  <el-table-column type="expand">
    <template v-slot="scope" >
      <div class="xiala">
        <div id="">
          <el-row v-for="(item1, index1) in scope.row.children" :key="item1.id" class="name1" :class="index1 != 0?'bdtop':''">
              <el-col :span="5">
              <el-tag closable @close="handleClose(scope.row.id, item1.id, scope)">
                {{item1.authName}}
              </el-tag>
              <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2, index2) in item1.children" :key="item2.id" class="name1" :class="index2 != 0?'bdtop':''">
                  <el-col :span="6" >
                    <el-tag type="success" closable @close="handleClose(scope.row.id, item2.id, scope)">
                      {{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                      <el-tag closable v-for="(item3, index3) in item2.children" :key="item3.id" type="warning" @close="handleClose(scope.row.id, item3.id, scope)">
                        {{item3.authName}}
                      </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
        </div>
     
        
      </div>
      
</template>
  </el-table-column>
  
  <el-table-column label="#" type="index"></el-table-column>
  <el-table-column label="角色名称" prop="roleName"></el-table-column>
  <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
  <el-table-column label="操作">
    <template v-slot="scope">
        <el-button icon="el-icon-edit" type="primary" size="mini" @click="editUserInfo(scope.row.id)">编辑</el-button>
      <el-button icon="el-icon-delete" type="danger" size="mini" @click="removeRolesById(scope.row.id)">删除</el-button>
      <el-button icon="el-icon-setting" type="warning" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>

    </template>
  </el-table-column>

</el-table>
  </el-card>
<!--添加角色-->
  <el-dialog
      title="添加角色"
      :visible.sync="addDialogVisible"
      @close="DialogClose('addDialogRef')">

    <el-form label-width="100px" ref="addDialogRef" :rules="addRolesRules" :model="addRolesForm">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="addRolesForm.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="addRolesForm.roleDesc"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
  </el-dialog>
<!--修改角色-->
  <el-dialog
      title="修改角色信息"
      :visible.sync="editDialogVisible"
      @close="DialogClose('editDialogRef')">

    <el-form label-width="100px" ref="editDialogRef" :rules="addRolesRules" :model="editUser">
      <el-form-item label="角色名称">
        <el-input v-model="editUser.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="email">
        <el-input v-model="editUser.roleDesc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUserClick">确 定</el-button>
  </span>
  </el-dialog>
<!--分配权限-->
  <el-dialog
      title="管理权限"
      :visible.sync="setRightDialogVisible"
      width="50%"
      @close="defKeys=[]">
    <el-tree :data="rightsList" ref="rights" :props="treeProps" show-checkbox node-key="id" default-expand-all	:default-checked-keys="defKeys"></el-tree>

    <span slot="footer" class="dialog-footer">
    <el-button @click="setRightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allToRights">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
import {remove} from "@/network/remove";

export default {
  name: "roles",
  data(){
    return{
      addDialogVisible:false,
      editDialogVisible:false,
      addRolesForm: {
        roleName: '',
        roleDesc: '',
      },
      addRolesRules: {
        roleName:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
    rolesList:[],
      editUser:{},
      setRightDialogVisible:false,
      rightsList:[],
      treeProps:{
        children: 'children',
        label: 'authName'
      },
      defKeys:[],
      rightsId:''
    }
  },
  components:{

  },
  created() {
    this.getRolesList()
  },
  methods:{
    async editUserInfo(id){
      const {data:res} = await this.$http.get(`roles/${id}`)
      // console.log(res)
      if (res.meta.status !== 200) return this.$Message.error("修改失败")
      this.editDialogVisible =true
      this.editUser = res.data
    },
    editUserClick(){
      this.$refs.editDialogRef.validate(async valid => {
        if (!valid) return
        const {data:res} = await this.$http.put(`roles/${this.editUser.roleId}`,{
          roleName:this.editUser.roleName,
          roleDesc:this.editUser.roleDesc})
        console.log(res)
        if (res.meta.status !== 200) return this.$Message.error("修改用户失败")
        this.$Message.success("修改用户成功")

        this.editDialogVisible = false
        await this.getRolesList()
      })
    },

    removeRolesById(id){
      // console.log(id)
      remove(`roles/${id}`, this.getRolesList)
    },
    DialogClose(ref){
      this.$refs[ref].resetFields()
    },
    addUser(){
      this.addInfo(this.$refs.addDialogRef, 'roles', this.addRolesForm, this.addDialogVisible = false, this.getRolesList)
    },
   async getRolesList(){
      const {data:res} = await this.$http.get('roles')
     if (res.meta.status !== 200) return this.$Message.error('出错辣')
     this.rolesList = res.data
     this.$Message.success('获取列表成功')
    },
   async showSetRightDialog(role){
      this.rightsId=role.id
      const {data:res} = await this.$http.get('rights/tree')
     if (res.meta.status !== 200) return this.$Message.error('出错辣')

     this.rightsList = res.data
     this.getDefKeys(role ,this.defKeys)
     this.setRightDialogVisible=true
    },
    getDefKeys(node, arr){
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getDefKeys(item, arr)
      })
    },
    async allToRights(){
    const keys = [
    ...this.$refs.rights.getCheckedKeys(),
    ...this.$refs.rights.getHalfCheckedKeys()
    ]
      const defkey = keys.join(',')

      const {data:res} =await this.$http.post(`roles/${this.rightsId}/rights`,{rids:defkey})
      if (res.meta.status !== 200) return this.$Message.error('出错辣')
      this.$Message.success('分配权限成功')

      await this.getRolesList()
      this.setRightDialogVisible=false
    },
  async handleClose(rolesId, id, index){
    
    const {data:res} = await remove(`roles/${rolesId}/rights/${id}`)
    // console.log(res);
    if(typeof res !=="undefined"){
      this.rolesList[index.$index].children = res
    }
    }
  }
}
</script>

<style scoped>
.rowBtn{
  margin-bottom: 15px;
}
.name1{
  display: flex;
  align-items: center;
}
.xiala{
  padding: 20px 50px !important;
}
.el-tag{
  margin: 7px;
}
.bdtop{
  border-top:1px solid #eee;
  }
</style>